from datetime import datetime
from configuration import DATE_FORMAT, TIMEOUT


def convert_str_to_datetime(date):
    return datetime.strptime(date, DATE_FORMAT)


class Timer(object):
    def __init__(self):
        self.start = datetime.now()
        self.end = None
        self.timeout = TIMEOUT

    def click(self):
        """
        Compute elapsed time
        :return: elapsed time count by second
        """
        self.end = datetime.now()

        return (self.end - self.start).seconds

    def reset(self):
        self.start = datetime.now()
        self.end = None

    def loading_timeout(self):
        """
        See the page loading whether exceeds the timeout
        :return:
        """
        if self.click() > self.timeout:
            self.reset()
            return True
        else:
            return False